<?php
/**
 * 方便调试、测试的接口
 */
declare(strict_types=1);

namespace App\Controller;

use Hyperf\HttpServer\Annotation\AutoController;
use App\Exception\ApiException;
use App\Constants\ErrorCode;
use Hyperf\DbConnection\Db;
use App\Model\AdminUserMenuAuth;
use App\Model\PointsRanking;
use App\Model\User;

/**
 * Class TestController
 * @package App\Controller\Test
 * @AutoController()
 */
class TestController extends AbstractController
{

    /**
     * 手动加密数据
     *
     * @return array|\Psr\Http\Message\ResponseInterface
     */
    public function encrypt_args()
    {
        $input = request()->all();
        // $input['type'] = 1;
        // $input['allocation_id'] = 28;
        // $input['sign'] = '2023-11-23 23:23:00';
        if (empty($input)) throw new ApiException(ErrorCode::PARAM_MISSING, '一个参数都没有，小老弟！');
        return ['data' => aes_encrypt($input)];
    }

    /**
     * 手动解密数据
     *
     * @return \Psr\Http\Message\ResponseInterface
     */
    public function decrypt_args()
    {
        if (!request()->has('data')) throw new ApiException(ErrorCode::PARAM_MISSING, '没有 data 参数，小老弟！');
        return aes_decrypt(request()->input('data'));
    }

    /**
     * 字符加密数据
     *
     * @return array|\Psr\Http\Message\ResponseInterface
     */
    public function encrypt_args_string()
    {
        $input = request()->input('string', '');
        if (empty($input)) throw new ApiException(ErrorCode::PARAM_MISSING, '没有 string 参数，，小老弟！');
        return aes_encrypt($input);
    }

    /**
     * 字符解密数据
     *
     * @return array|\Psr\Http\Message\ResponseInterface
     */
    public function decrypt_args_string()
    {
        $input = request()->input('string', '');
        if (empty($input)) throw new ApiException(ErrorCode::PARAM_MISSING, '一个参数都没有，小老弟！');
        return aes_decrypt($input);
    }

    //用户积分转星币临时脚本
    public function user_points_star_currency()
    {
        $userList = User::query()->get()->toArray();
        foreach($userList as $val)
        {
            $star_currency = $val['star_currency'] + $val['points'];
            User::query()->where('id', $val['id'])->update(['star_currency' => $star_currency, 'points' => 0]);
        }
    }

}
